/**
 * 
 */
package com.py.credit.service.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.alibaba.fastjson.JSONArray;
import com.py.credit.common.ServiceException;
import com.py.credit.dao.TableDao;
import com.py.credit.dao.ViewInfoDao;
import com.py.credit.entity.Column;
import com.py.credit.entity.TableResult;
import com.py.credit.entity.ViewInfo;
import com.py.credit.entity.common.OptionalColumn;
import com.py.credit.service.ViewInfoService;
import com.py.credit.utils.SQLBuilder;
import com.py.framework.core.common.Page;

/**
 * @author dd
 *
 */
@Service
public class ViewInfoServiceImpl implements ViewInfoService {

	private static final Logger LOGGER = LoggerFactory.getLogger(ViewInfoServiceImpl.class);

	@Autowired
	private ViewInfoDao viewInfoDao;
	@Autowired
	private TableDao tableDao;

	@Override
	@Transactional
	public void addView(ViewInfo viewInfo, String tableColumnInfo) {

		if (StringUtils.isBlank(viewInfo.getName())) {

			throw new ServiceException("视图名不能为空");

		}

		List<TableResult> tableResultList = JSONArray.parseArray(tableColumnInfo, TableResult.class);
		if (CollectionUtils.isEmpty(tableResultList)) {

			throw new ServiceException("至少勾选一列");
		}

		if (viewInfoDao.selectByViewName(viewInfo.getName(), null) > 0) {

			throw new ServiceException("存在相同的视图名");
		}

		viewInfoDao.add(viewInfo);

		String createViewQuery = SQLBuilder.createViewQuery(viewInfo.getName(), viewInfo.getRootId(), tableResultList);
		LOGGER.debug("VIEW CREATE  ==============>" + createViewQuery);

		for (TableResult tableResult : tableResultList) {

			List<Column> columns = tableResult.getColumns();

			for (Column col : columns) {
				viewInfoDao.addViewColumn(tableResult.getTableNameCN(), tableResult.getTableNameEN(), col.getAliases(),
						col.getMetricId(), col.getMetricCode(), col.getMetricName(), viewInfo.getId());
			}

		}

		tableDao.operate(createViewQuery);

	}

	@Override
	public List<Map<String, Object>> getTableListForPage(String kId, List<String> showColumns,
			String[] additioanlColumns, String condition, Page page) {
		if (CollectionUtils.isEmpty(showColumns)) {

			throw new ServiceException("至少选择一列");

		}
		List<String> conditions = SQLBuilder.createConditionSql(condition);
		return viewInfoDao.selectTableListByConditionForPage(kId, showColumns, additioanlColumns, conditions, page);
	}

	@Override
	public List<OptionalColumn> getViewResultOptionalColums(String viewId) {

		return viewInfoDao.selectViewResultOptionalColums(viewId);
	}

	@Override
	public List<OptionalColumn> getViewResultAdvancedConditionColumns(String viewId) {

		return viewInfoDao.selectViewResultAdvancedConditionColumns(viewId);

	}

	@Override
	public List<Map<String, Object>> getViewResultForPage(String viewName, List<String> showColumns,
			String[] additioanlColumns, String condition, Page page) {

		if (CollectionUtils.isEmpty(showColumns)) {

			throw new ServiceException("至少选择一列");

		}
		List<String> conditions = SQLBuilder.createConditionSql(condition);

		return viewInfoDao.selectViewResultByConditionForPage(viewName, showColumns, additioanlColumns, conditions,
				page);
	}

	@Override
	public List<String> getDefaultShowColumnEN(String viewId) {

		return viewInfoDao.selectDefaultShowColumnEN(viewId);
	}

	@Override
	@Transactional
	public void updateView(ViewInfo viewInfo, String tableColumnInfo) {
		List<TableResult> tableResultList = JSONArray.parseArray(tableColumnInfo, TableResult.class);
		if (CollectionUtils.isEmpty(tableResultList)) {

			throw new ServiceException("至少勾选一列");
		}

		if (viewInfoDao.selectByViewName(viewInfo.getName(), viewInfo.getId()) > 0) {

			throw new ServiceException("存在相同的视图名");
		}

		List<String> names = viewInfoDao.selectViewNameByIds(new String[] { String.valueOf(viewInfo.getId()) });

		viewInfoDao.update(viewInfo);

		viewInfoDao.dropView(names.get(0));

		viewInfoDao.delViewMetric(new String[] { String.valueOf(viewInfo.getId()) });

		String createViewQuery = SQLBuilder.createViewQuery(viewInfo.getName(), viewInfo.getRootId(), tableResultList);
		LOGGER.debug("VIEW CREATE  ==============>" + createViewQuery);

		for (TableResult tableResult : tableResultList) {

			List<Column> columns = tableResult.getColumns();

			for (Column col : columns) {
				viewInfoDao.addViewColumn(tableResult.getTableNameCN(), tableResult.getTableNameEN(), col.getAliases(),
						col.getMetricId(), col.getMetricCode(), col.getMetricName(), viewInfo.getId());
			}

		}

		tableDao.operate(createViewQuery);

	}

	@Override
	@Transactional
	public void del(String[] ids) {

		List<String> names = viewInfoDao.selectViewNameByIds(ids);

		for (String name : names) {
			viewInfoDao.dropView(name);
		}

		viewInfoDao.delView(ids);

		viewInfoDao.delViewMetric(ids);

	}

	@Override
	public List<TableResult> getDetail(String viewId) {

		return viewInfoDao.selectDetail(viewId);
	}

	@Override
	public ViewInfo getViewInfoById(String viewId) {

		return viewInfoDao.selectViewInfoById(viewId);
	}

}
